package com.zzh.core;

import com.zzh.lib.core.utils.LogUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by ZZH on 2021/9/16.
 *
 * @Date: 2021/9/16
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: 网络监测轮询线程
 */
public class NetWatchRunnable implements Runnable {
    public boolean watchNet = true;
    BufferedReader buf = null;
    BufferedReader reader = null;

    @Override
    public void run() {
        LogUtils.e("-------开始");
        try {
            Process exec = Runtime.getRuntime().exec("ping -i 1 -s 512 -t 255 172.17.12.12");
            while (watchNet){
                buf = new BufferedReader(new InputStreamReader(exec.getInputStream()));
                reader = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
                String str = new String();
                while ((str = buf.readLine()) != null) {
                    LogUtils.d("---------执行结果: " + str);
                }
                /*while ((str = reader.readLine()) != null) {
                    LogUtils.d("---------执行结果2: " + str);
                }*/
            }
        } catch (IOException e) {
            LogUtils.e("-------检测网络失败");
        } finally {
            LogUtils.e("-------finally");
            try {
                if (buf != null) {
                    buf.close();
                }
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}